package multigest.vocappulary;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class CatalogOverviewActivity extends ListActivity {

	private static final String TAG_NAME = "name";
	private static final String TAG_LRESULT = "lastResult";
	private static final String TAG_LDATE = "lastDate";
	
	//String for choosen modus
	private String modus = null;
	
	//String for the found packages
	String[]vocpackages = null;
	
    //Arraylist of Hashmaps for ListView
    ArrayList<HashMap<String, String>> packageList = new ArrayList<HashMap<String, String>>();
		
	
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Getting modus (test or practice)
		modus = getIntent().getStringExtra("modus");
		
		JSONObject json = null;
		
		//Read all filenames in asset-folder "Vocabulary"
		try{
			vocpackages = getAssets().list("Vocabulary");
		}catch(IOException e){			
			e.printStackTrace();
		}	
	
		//Getting information of all json files
		for(int i= 0; i < vocpackages.length; i++){
			try {
				json = VocAppUtils.loadJSONFromAsset("Vocabulary/"+vocpackages[i], getAssets());
			
				String name = json.getString(TAG_NAME);
				String lastResult = "Last Result: "+json.getString(TAG_LRESULT)+"%";
				String lastDate = "Last Date: "+json.getString(TAG_LDATE);
				
				//creating new HashMap
				HashMap<String, String> hmap = new HashMap<String, String>();
			
				hmap.put(TAG_NAME, name);
				hmap.put(TAG_LRESULT, lastResult);
				hmap.put(TAG_LDATE, lastDate);
				
				packageList.add(hmap);
						
			} catch (JSONException e) {
			
	           Builder jsonErr = new AlertDialog.Builder(this);
	           
	           jsonErr.setTitle("JSON Error");
	           jsonErr.setMessage("An error occurred while loading a vocabulary package!("+vocpackages[i]+")");
	           jsonErr.setNeutralButton("OK", new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int which) {
	                    //Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
	                }
	            });
	 
	            // Showing Alert Message
	            jsonErr.show();			
			
	            e.printStackTrace();
			}
		}
		
		
		ListAdapter ladapter = new SimpleAdapter(this, packageList, R.layout.activity_catalog, new String[]{TAG_NAME, TAG_LDATE, TAG_LRESULT},
									new int[]{R.id.packageName, R.id.lastDate, R.id.lastResult});
		
		this.setListAdapter(ladapter);
		
		ListView lview = getListView();
		
		lview.setOnItemClickListener(new OnItemClickListener(){
 
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                
            	if(modus.equals("practice")){
	            	// getting values from selected ListItem
	            	Intent intent = new Intent(getApplicationContext(), VocPracticeActivity.class);
	            	intent.putExtra("catalog", vocpackages[position]);
			    	startActivity(intent);
            	}else{
	            	// getting values from selected ListItem
	            	Intent intent = new Intent(getApplicationContext(), VocTestActivity.class);
	            	intent.putExtra("catalog", vocpackages[position]);
			    	startActivity(intent);
            	}
            }
		});        
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.catalog_overview, menu);
		return true;
	}

}
